package com.tongji.showroom.view
{ 
	import com.tongji.showroom.events.AddUserProductEvent;
	import com.tongji.showroom.events.DeleteObjEvent;
	import com.tongji.showroom.events.GetSystemSceneEvent;
	import com.tongji.showroom.events.GetTypesEvent;
	import com.tongji.showroom.events.NavigatorEvent;
	import com.tongji.showroom.events.SearchIdEvent;
	import com.tongji.showroom.events.SearchSceneEvent;
	import com.tongji.showroom.events.ShowRoomEvent;
	import com.tongji.showroom.model.GetSystemSceneModel;
	import com.tongji.showroom.model.SearchSceneModel;
	import com.tongji.showroom.model.TypeModel;
	import com.tongji.showroom.model.UserModel;
	import com.tongji.showroom.utils.PageTools;
	
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.INavigatorContent;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 
	 * @author jiarong
	 * 
	 */
	public class SecondStepSelectSenceMediator extends Mediator
	{
		[Inject]
		public var view:SecondStepSelectSenceComp;
		
		[Inject]
		public var typemodel:TypeModel;
		
		[Inject]
		public var searchSceneModel:SearchSceneModel;
		
		[Inject]
		public var getSceneModel:GetSystemSceneModel;
		
		[Inject]
		public var userModel:UserModel;
		
		public var cdIdArr:Array = new Array();
		public var fgIdArr:Array = new Array();
		
		private var tempNumber:Number = 0;
		private var leftIndex:Number = 0;
		private var rightIndex:Number = 3;
		private var pageIndex:Number = 1;
		public function SecondStepSelectSenceMediator()
		{
			super();
		}
		
		override public function onRegister():void
		{
			dispatch( new AddUserProductEvent(AddUserProductEvent.ADD_USER_PRODUCT, userModel.userID));
			addContextListener(GetTypesEvent.UPADATE_PLACES, getTypeHandle, GetTypesEvent);
			addContextListener(GetTypesEvent.UPDATE_STYLES, getTypeHandle, GetTypesEvent);
			
			addViewListener(SearchIdEvent.SELECTED_SCENE, selectSceneHandler, SearchIdEvent);
			view.rightBtn.addEventListener(MouseEvent.CLICK, rigthClickHandler);
			view.leftBtn.addEventListener(MouseEvent.CLICK, leftClickHandler);
			view.addEventListener(DeleteObjEvent.SECOND_SCENE_DELETE_PIC, deleteScene);
			view.searchBtn.addEventListener(MouseEvent.CLICK, searchSceneHandler);
			view.placesList.addEventListener(SearchIdEvent.SEARCH_CDID, searchSceneIdHandler);
			view.stylesList.addEventListener(SearchIdEvent.SEARCH_FGID, searchStyleIdHandler);
			view.placesList.addEventListener(SearchIdEvent.DELETE_CDID, deletecdIdHandler);
			view.stylesList.addEventListener(SearchIdEvent.DELETE_FGID, deletefgIdHandler);
			
			BindingUtils.bindProperty(view.selectScene.selectSceneList, "dataProvider", getSceneModel, "getSceneArr");	
			BindingUtils.bindProperty(view.placesList.dataGroup, "dataProvider", typemodel, "placeCollection");
			BindingUtils.bindProperty(view.stylesList.dataGroup, "dataProvider", typemodel, "styleCollection");		
			mediatorMap.createMediator(new SelectSenceDataListItemRenderer());
			addContextListener(SearchSceneEvent.UPDATE_SCENES, setpage, SearchSceneEvent);
			addViewListener(NavigatorEvent.PAGE_INDEX_CHANGE, changePageHandler, NavigatorEvent);			

			var page:PageTools = new PageTools(getSceneModel.recordCount, 3, 9, pageIndex);
			view.selectScene.pageComp.createDataProvider([page.nums, page.pageArray, page.pageCount, pageIndex]);
		}
		private function setpage(event:SearchSceneEvent):void
		{
			var page:PageTools = new PageTools(getSceneModel.recordCount, 3, 9, pageIndex);
			view.selectScene.pageComp.createDataProvider([page.nums, page.pageArray, page.pageCount, pageIndex]);
		}
		
		private function changePageHandler(event:NavigatorEvent):void
		{			
			pageIndex = event.pageIndex;
			dispatch(new SearchSceneEvent(SearchSceneEvent.SEARCH_ALL_SCENES, userModel.userID, "","",event.pageIndex, 9));
			/*var pageTools:PageTools = new PageTools(getSceneModel.recordCount, 3, 9, event.pageIndex);
			view.selectScene.pageComp.createDataProvider([pageTools.nums, pageTools.pageArray, pageTools.pageCount, pageTools.pageIndex]);*/
		}
		
		//获取与场地中所选checkBox对应的对象的场景Id
		private function searchSceneIdHandler(event:SearchIdEvent):void
		{
			cdIdArr.push(event.scdId);
			for (var i:int = 0; i < typemodel.placeCollection.length; i++)
			{
				if (typemodel.placeCollection.getItemAt(i).typeId == event.scdId)
				{
					typemodel.placeCollection.getItemAt(i).selected = true;
					break;
				}
			}
		}
		
		//获取与风格中所选checkBox对应的对象的风格Id
		private function searchStyleIdHandler(event:SearchIdEvent):void
		{
			fgIdArr.push(event.sfgId);
			for (var i:int = 0; i < typemodel.styleCollection.length; i++)
			{
				if (typemodel.styleCollection.getItemAt(i).typeId == event.scdId)
				{
					typemodel.styleCollection.getItemAt(i).selected = true;
					break;
				}
			}
		}
		
		//删除与场地中所选checkBox对应的对象的场景Id
		private function deletecdIdHandler(event:SearchIdEvent):void
		{ 
			for(var i:int = 0; i < cdIdArr.length; i++)
			{
				if(event.scdId == cdIdArr[i])
				{
					cdIdArr.splice(i, 1);
				}
			}
			for (var j:int = 0; j < typemodel.placeCollection.length; j++)
			{
				if (typemodel.placeCollection.getItemAt(j).typeId == event.scdId)
				{
					typemodel.placeCollection.getItemAt(j).selected = false;
					break;
				}
			}
		}
		
		//删除与风格中所选checkBox对应的对象的风格Id
		private function deletefgIdHandler(event:SearchIdEvent):void
		{
			for(var i:int = 0; i < fgIdArr.length; i++)
			{
				if(event.sfgId == fgIdArr[i])
				{
					fgIdArr.splice(i, 1);
				}
			}
			for (var j:int = 0; j < typemodel.styleCollection.length; j++)
			{
				if (typemodel.styleCollection.getItemAt(j).typeId == event.scdId)
				{
					typemodel.styleCollection.getItemAt(j).selected = false;
					break;
				}
			}
		}
		
		//根据场景和风格搜索所需条件的场景图片并显示
		private function searchSceneHandler(event:MouseEvent):void
		{
			var cdIdStr:String = cdIdArr[0];
			var fgIdStr:String = fgIdArr[0];
			
			for(var i:int = 1; i < cdIdArr.length; i++)
			{
				if(i == 0)
				{
					cdIdStr = cdIdArr[i];
				}
				else
				{
					cdIdStr = cdIdStr +"," + cdIdArr[i] as String;
				}
			}
			
			for(var j:int = 1; j < fgIdArr.length; j++)
			{
				if(j == 0)
				{
					fgIdStr = fgIdArr[j];
				}
				else
				{
					fgIdStr = fgIdStr + "," + fgIdArr[j] as String;
				}
			}
			dispatch(new SearchSceneEvent(SearchSceneEvent.SEARCH_ALL_SCENES, userModel.userID, cdIdStr, fgIdStr, 1, 9, null));
			dispatch( new GetSystemSceneEvent(GetSystemSceneEvent.GET_ALL_SCENE, cdIdStr, fgIdStr, 1, 9, null));
			BindingUtils.bindProperty(view.selectScene.selectSceneList, "dataProvider", searchSceneModel, "SearchSceneArr");
		}
		
		//删除已选择场景中的场景图片
		private function deleteScene(event:DeleteObjEvent):void
		{
			var location:int = searchSceneModel.selectSceneArray.getItemIndex(event.obj);
			searchSceneModel.selectSceneArray.removeItemAt(location);
		}
		
		//右击显示已选择场景未显示的场景图片
		private function rigthClickHandler(event:MouseEvent):void
		{
			if(rightIndex < searchSceneModel.selectSceneArray.length - 1)
			{
				rightIndex++;
				leftIndex++;
				view.showSceneList.ensureIndexIsVisible(rightIndex);
			}
		}
		
		//左击显示已选择场景未显示的场景图片
		private function leftClickHandler(event:MouseEvent):void
		{
			if(leftIndex > 0)
			{
				leftIndex--;
				rightIndex--;
				view.showSceneList.ensureIndexIsVisible(leftIndex);
			}
		}
		
		//选择场景图片显示在已选择场景中
		private function selectSceneHandler(event:SearchIdEvent):void
		{
			searchSceneModel.selectSceneArray.refresh();
			var obj:Object = event.sceneObject;
			if(tempNumber < 12)
			{
				if( !searchSceneModel.selectSceneArray.contains(obj))
				{
					searchSceneModel.selectSceneArray.addItem(obj);
					tempNumber++;
				}
				else
				{
					Alert.show("您已经选择了！");
				}
				for (var i:int = 0; i < searchSceneModel.selectSceneArray.length; i++)
				{
					for (var j:int = i + 1; j < searchSceneModel.selectSceneArray.length; j++)
					{
						if (searchSceneModel.selectSceneArray.getItemAt(i).ImgUrl == searchSceneModel.selectSceneArray.getItemAt(j).ImgUrl)
						{
							searchSceneModel.selectSceneArray.removeItemAt(j);
							tempNumber--;
							Alert.show("您已经选择了！");
							j--;
						}
					}
				}
			}
			else
			{
				Alert.show("您已经选择了12个场景，不能再选了！");
			}
			BindingUtils.bindProperty(view.showSceneList, "dataProvider", searchSceneModel, "selectSceneArray");
		}
		
		//绑定数据到场地和风格中
		private function getTypeHandle(event:GetTypesEvent):void
		{
			BindingUtils.bindProperty(view.placesList.dataGroup, "dataProvider", typemodel, "placeCollection");
			
			BindingUtils.bindProperty(view.stylesList.dataGroup, "dataProvider", typemodel, "styleCollection");		
		}
		
	}
}